
#include "system_gd32f10x.h"
#include "gd32f10x_conf.h"
#include "osObjects.h"  
#include "systimer.h"
#include "mbdatacb.h"
#include "peripheral.h"
#include "at24xx.h" 
#include "appconfig.h"
#include "iap.h"
#include "dataproc.h"
#include "time.h"

//---------------------------------------------------------------------------//
void LedControl(void);
void KeyCheck(void);
/*----------------------------------------------------------------------------
 *      Global Functions
 *---------------------------------------------------------------------------*/
 
/*--------------------------- os_idle_demon ---------------------------------*/

uint32_t uuid[3];
uint32_t memInfo;

uint32_t IdleTaskCnt;

uint8_t KeyVal;
uint8_t KeyValLast;
uint8_t KeyTestLast;
uint8_t KeyTimeCnt;

/// \brief The idle demon is running when no other thread is ready to run
void os_idle_demon (void)
{
    TIME_Type  TimeX;
	osStatus status;
	
	uuid[0] = *((const uint32_t *) 0x1FFFF7AC);
	uuid[1] = *((const uint32_t *) 0x1FFFF7B0);
	uuid[2] = *((const uint32_t *) 0x1FFFF7B4);
	memInfo = *((const uint32_t *) 0x1FFFF7E0);
	
	SysTickInit();
    
    TimeX.Year = 2016;
    TimeX.Mon  = 6;
    TimeX.Day  = 28;
    TimeX.Hour = 12;
    TimeX.Min  = 30;
    TimeX.Sec  = 30;
    status = osMutexWait (mid_TimeMutex, NULL);
    if(osOK == status){TimeSet(&TimeX);}else{}
    osMutexRelease (mid_TimeMutex);
    
    for (;;)
	{
		SysPulseClear();
		SysPulseCreat();
		
		//---------------------------------------------------------------------//
		// HERE: include optional user code to be executed when no thread runs.
		IdleTaskCnt++;
		
        // I2C 
		status = osMutexWait (mid_I2cMutex, NULL);
		if(osOK == status)
		{

		}
		else
		{
			
		}
		osMutexRelease (mid_I2cMutex);
		
        //Time 
        if(bPulse_1s)
        {
            status = osMutexWait (mid_TimeMutex, NULL);
            if(osOK == status){TimeRun();}else{}
            osMutexRelease (mid_TimeMutex);
        }
        
		LedControl();
		KeyCheck();
	}
}

/*****************************************************************************//*!
* @brief   	led control   .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void LedControl()
{
		if(bPulse_250ms)
		{	
			if(LedSt(LED_RUN))
				LedOff(LED_RUN);
			else
				LedOn(LED_RUN);
            
            if(LedSt(LED_WIFI))
				LedOff(LED_WIFI);
			else
				LedOn(LED_WIFI);
		}
		
        //modbus led control
		{
            if(mbState.bits.PortSt)
            {
                if(mbState.bits.Connect)
                {
                    if(bPulse_125ms)
                    {
                        if(LedSt(LED_COM))
                            LedOff(LED_COM);
                        else
                            LedOn(LED_COM);                    
                    }                    
                }
                else
                {
                    if(bPulse_2s)
                    {
                        if(LedSt(LED_COM))
                            LedOff(LED_COM);
                        else
                            LedOn(LED_COM);                    
                    }
                }
            }
            else
            {
                LedOn(LED_COM);  
            }
		}
		
}


/*****************************************************************************//*!
* @brief   	Key check  .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void KeyCheck()
{
	uint8_t KeyCur;
	
	if(bPulse_20ms)
	{
		if(KeyGet(KEY))
		{
			KeyCur = 1;
		}
		else
		{
			KeyCur = 0;
		}
		
		if(KeyCur == KeyTestLast)
		{
			KeyVal = KeyCur;
		}
		
		KeyTestLast = KeyCur;
	}
	
	if((KeyValLast==0) && (KeyVal==1))
	{
		//First key down
		
	}
	else if((KeyValLast==1) && (KeyVal==0))
	{
		//First Key up
	}
	else
	{
		//up or down long time
		if(KeyVal)
		{
			//key down
		}
		else
		{
			//key up
			KeyTimeCnt = 0;
		}
	}
	
	KeyValLast = KeyVal;
}
